using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using Model.Database;

namespace Model.Mapping
{
    public class DepartmentMap : EntityTypeConfiguration<Department>
    {
        public DepartmentMap()
        {
            // Primary Key
            this.HasKey(t => t.DepartmentId);

            // Properties
            this.Property(t => t.DepartmentId)
                .IsRequired()
                .HasMaxLength(32);

            this.Property(t => t.ParentId)
                .IsRequired()
                .HasMaxLength(32);

            this.Property(t => t.DepartmentName)
                .IsRequired()
                .HasMaxLength(25);

            this.Property(t => t.Description)
                .HasMaxLength(50);

            // Table & Column Mappings
            this.ToTable("Department");
            this.Property(t => t.DepartmentId).HasColumnName("DepartmentId");
            this.Property(t => t.ParentId).HasColumnName("ParentId");
            this.Property(t => t.DepartmentName).HasColumnName("DepartmentName");
            this.Property(t => t.Description).HasColumnName("Description");
            this.Property(t => t.Sort).HasColumnName("Sort");
            this.Property(t => t.AddTime).HasColumnName("AddTime");
            this.Property(t => t.ModifyTime).HasColumnName("ModifyTime");

            // Relationships
            this.HasMany(t => t.Roles)
                .WithMany(t => t.Departments)
                .Map(m =>
                    {
                        m.ToTable("R_Department_Role");
                        m.MapLeftKey("Department_DepartmentId");
                        m.MapRightKey("Role_RoleId");
                    });

            this.HasMany(t => t.UserInfoes)
                .WithMany(t => t.Departments)
                .Map(m =>
                    {
                        m.ToTable("R_UserInfo_Department");
                        m.MapLeftKey("Department_DepartmentId");
                        m.MapRightKey("UserInfo_UserInfoId");
                    });


        }
    }
}
